<?php

class Sgit_Form_Element_Select extends Zend_Form_Element_Select
{
    protected $opciones;
    
    public function __construct($spec, $options = null,$filter=null,$where=null)
    {
        $this->where=$where;
        
        parent::__construct($spec, $options);
        

        
        $this->addFilter('Null');
        
        if($this->opciones)
            $this->setMultiOptions( $this->opciones);
            
        $this->_setInitValues();
        $this->_filterOptions($filter);
        $this->_firstBlank();
        $this->setAttrib('style',"max-width:180px");
        unset($this->opciones);
        unset($this->where);
    }
    

    
    
    protected function _setInitValues()
    {
        $request= Zend_Controller_Front::getInstance()->getRequest();
        if($request->isGet() && $request->has($this->getName())){
            $value = $request->getParam($this->getName());
            $this->setValue($value);
        }
    }

    protected function _filterOptions($filter)
    {
        if(!is_null($filter)){
            $old_options=$this->getMultiOptions();
            foreach((array)$filter as $i){
                $options[$i]=$old_options[$i];
            }
            $this->setMultiOptions( $options);
        }

    }
    
    protected function _firstBlank()
    {
        
        if($this->getAttrib('firstBlank')!==false){
            $options=$this->getMultiOptions();
            
            $newoptions[""] = "";
            foreach ((array)$options as $k => $v)  
                $newoptions[$k] = $v;
            $this->setMultiOptions( $newoptions);
        }
    }
    
    public function firstBlank()
    {
       
       $options=$this->getMultiOptions();
            
       $newoptions[""] = "";
       foreach ((array)$options as $k => $v)  
        $newoptions[$k] = $v;
       $this->setMultiOptions( $newoptions);
        
    }
    
    /**
     * 
     * Sobreescribe el metodo heredado, permite que se pueda deshabilitar 
     * translate solo en opciones y no en label
     * 
     * 
     */ 
    public function getTranslator()
    {
        /*
        if ($this->translatorIsDisabled()) {
            return null;
        }
        */
        if (null === $this->_translator) {
            return Zend_Form::getDefaultTranslator();
        }
        return $this->_translator;
    }
    
    
}


